package org.lex.ftp.ui;

import java.awt.event.KeyEvent;

import javax.swing.*;

import org.lex.explorer.ToggleSplitPaneAction;
import org.lex.ftp.FTP;
import org.lex.ftp.ui.actions.AboutAction;
import org.lex.ftp.ui.actions.BookmarkAction;
import org.lex.ftp.ui.actions.FileRulesAction;
import org.lex.ftp.ui.actions.SpeedAction;
import org.lex.ftp.util.Actions;


public class MainMenuBar extends JMenuBar {
	private final FTP app;
	private final ActionMap am;
	// -------------------------------
	private JMenu sessionMenu = null;
	private JMenu viewMenu = null;
	private JMenu toolMenu = null;
	private JMenu optionMenu = null;
	private JMenu helpMenu = null;

	public MainMenuBar(FTP app) {
		super();
		this.app = app;
		am = app.getActionMap();
		this.initialize();
	}

	private void initialize() {
		this.add(this.getSessionMenu());
		this.add(this.getViewMenu());
		this.add(this.getOptionMenu());
		this.add(this.getToolMenu());
		this.add(this.getHelpMenu());
	}

	private JMenu getSessionMenu() {
		if (sessionMenu == null) {
			sessionMenu = new JMenu("Session");
			sessionMenu.setMnemonic(KeyEvent.VK_C);
			sessionMenu.add(am.get(Actions.CONNECT));
			sessionMenu.add(am.get(Actions.DISCONNECT));
			sessionMenu.addSeparator();
			sessionMenu.add(am.get(Actions.EXIT));
		}
		return sessionMenu;
	}

	private JMenu getViewMenu() {
		if (null == this.viewMenu) {
			viewMenu = new JMenu("View");
			viewMenu.setMnemonic(KeyEvent.VK_V);
			viewMenu.add(new JCheckBoxMenuItem(new ToggleAction(app.getContentPane().getTopSplitPane())));
			// viewMenu.add( new lex.swing.plaf.menu.LookAndFeelMenu() );
		}
		return this.viewMenu;
	}

	private JMenu getToolMenu() {
		if (null == this.toolMenu) {
			toolMenu = new JMenu("Tool");
			toolMenu.add(new BookmarkAction(app));
			toolMenu.setMnemonic(KeyEvent.VK_T);
			toolMenu.add(am.get(Actions.FIND));
			toolMenu.add(am.get(Actions.CALCULATE_SIZE));
			toolMenu.add(new SpeedAction());
			toolMenu.addSeparator();
			toolMenu.add(am.get(Actions.NEW_SESSION));
		}
		return this.toolMenu;
	}

	private JMenu getOptionMenu() {
		if (null == optionMenu) {
			optionMenu = new JMenu("Options");
			optionMenu.setMnemonic(KeyEvent.VK_O);
			optionMenu.add(new FileRulesAction(app));
			optionMenu.add(am.get(Actions.PREFERENCES));
		}
		return optionMenu;
	}

	@Override
	public JMenu getHelpMenu() {
		if (null == helpMenu) {
			helpMenu = new JMenu("Help");
			helpMenu.setMnemonic(KeyEvent.VK_H);
			helpMenu.add(am.get(Actions.SESSIONS));
			helpMenu.addSeparator();
			helpMenu.add(new AboutAction(app));
		}
		return helpMenu;
	}

	static class ToggleAction extends ToggleSplitPaneAction {
		public ToggleAction(JSplitPane split) {
			super(split, "Show File View", "Hide File View");
			this.setIcon(null);
			this.setSelected(true);
			this.setName("File Explorer");
		}
	}
}
